package com.platform.comm.jwt;


import com.platform.comm.constants.GlobalConstant;
import com.platform.comm.util.UserConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * describe  根据token获取用户信息
 *
 * @author admin
 */
@Slf4j
@Component
public class TokenInfoUtil {

    private final JwtTokenUtil jwtTokenUtil;

    public TokenInfoUtil(JwtTokenUtil jwtTokenUtil) {
        this.jwtTokenUtil = jwtTokenUtil;
    }

    public JwtTokenUtil.JwtUser getUser(HttpServletRequest request) {
        String token = getToken(request);
        if (StringUtils.isNotBlank(token)) {
            return jwtTokenUtil.getUserFromToken(token);
        }
        return null;
    }

    public JwtTokenUtil.JwtUser getUser() {
        JwtTokenUtil.JwtUser jwtUser = new JwtTokenUtil.JwtUser();
        jwtUser.setId(UserConstant.ADMIN_USER);
        jwtUser.setUsername("admin");
        jwtUser.setRealName("超级管理员");
        jwtUser.setDepartId(GlobalConstant.HE_BEI_DEAPRT);
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        RequestContextHolder.setRequestAttributes(requestAttributes,true);
        if (Objects.isNull(requestAttributes)) {
            return jwtUser;
        }
        HttpServletRequest request = requestAttributes.getRequest();
        String token = getToken(request);
        if (StringUtils.isNotBlank(token)) {
            return jwtTokenUtil.getUserFromToken(token);
        }
        return jwtUser;
    }

    public String getUserId(){
        return getUser().getId();
    }

    public String getToken(HttpServletRequest request) {
        // accessToken修改为Authorization
        String authorization = request.getHeader("Authorization");
        if (authorization != null && authorization.contains("Bearer")) {
            return authorization.substring("Bearer".length() + 1);
        }
        return null;
    }

    public String getToken(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        RequestContextHolder.setRequestAttributes(requestAttributes,true);
        if (Objects.isNull(requestAttributes)) {
            return null;
        }
        HttpServletRequest request = requestAttributes.getRequest();
        return getToken(request);
    }

    public void initUserId(List<Consumer<String>> consumers) {
        JwtTokenUtil.JwtUser jwtUser = getUser();
        if (Objects.nonNull(jwtUser) && Objects.nonNull(consumers)) {
            consumers.forEach(consumer -> consumer.accept(jwtUser.getId()));
        }
    }

    public void initUserIdAndDeptId(List<Consumer<String>> consumers, Consumer<String> dept) {
        JwtTokenUtil.JwtUser jwtUser = getUser();
        if (Objects.nonNull(jwtUser) && Objects.nonNull(consumers)) {
            consumers.forEach(consumer -> consumer.accept(jwtUser.getId()));
        }
        if (Objects.nonNull(jwtUser) && Objects.nonNull(dept)) {
            dept.accept(jwtUser.getDepartId());
        }
    }

}
